package com.burgeon.backend.SHOP.rest;
/**
 * 小程序主页兑换
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CreateAppCoupon extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateAppCoupon(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符
     */

    public String getData(String code, String price, String vip, String original,
                        String datebeg, String dateend,
                        List<String> goods,List<String> sotreCode) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(code, price, vip, original, datebeg, dateend,goods,sotreCode), url);
        System.err.println(str);
        String rcode = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(rcode)) {
            throw new Exception(message);
        }

        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(String code, String price, String vip, String original,
                               String datebeg, String dateend,
                               List<String> goods,List<String> sotreCode) throws Exception {


        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "C_V_VOUCHERS");
        masterobj.put("ID", "-1");

        masterobj.put("VOUCHERS_NO", "'" + code + "'");
        masterobj.put("VOU_TYPE", "VOU5");
        masterobj.put("IDENTIFY_CODE","'" + code + "'");
        masterobj.put("AMT_DISCOUNT","'" + price + "'");
        masterobj.put("C_VIP_ID__CARDNO",vip);
        masterobj.put("AMT_NOLES",original);
        masterobj.put("START_DATE",datebeg);
        masterobj.put("VALID_DATE",dateend);
        masterobj.put("IS_ONETIME", "Y");//是否一次性
        masterobj.put("IS_ALLSTORE", "Y");//是否应用于所有店铺
        masterobj.put("DESCRIPTION", "线上积分抵现");//备注
        masterobj.put("ISACTIVE", "Y");//是否可用
        masterobj.put("ACCOUNT_LIMIT", "5000");
        masterobj.put("ISSHARE_PAYTYPE", "Y");
        masterobj.put("IS_DELACCOUNT", "Y");
        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        FIndAD_Table fIndAD_table = new FIndAD_Table(ip,account,password);
        HashMap yc_retail = fIndAD_table.getData("C_V_VOUCHERS");

        list.add(yc_retail.get("C_VOUDISLIMIT"));//4435
      //  list.add(yc_retail.get("C_VOUCHERS_STORE"));//4433


        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "C_VOUDISLIMIT");//限制商品

        List addList1 = new ArrayList();
        for (String goodsCode : goods) {
            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("M_PRODUCT_ID__NAME", goodsCode);

            addList1.add(map1);
        }

        if (goods.size()>0){
            ref1.put("addList", String.valueOf(addList1));
        }

        refobjs.add(ref1);

        //**************************************************************
        HashMap ref2 = new HashMap();
        ref2.put("table", "C_VOUCHERS_STORE");//限制商品


        List addList2 = new ArrayList();
        for (String store : sotreCode) {
            HashMap map2 = new HashMap();//addlist内容
            map2.put("id", "-1");
            map2.put("C_STORE_ID__NAME", store);

            addList2.add(map2);
        }

        if (sotreCode.size()>0){
            ref2.put("addList", String.valueOf(addList2));
        }

        refobjs.add(ref2);
//**********************************************************************



        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        //params.put("submit", "true");//是否提交


        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateAppCoupon createAppCoupon = new CreateAppCoupon("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {

            List<String> g = new ArrayList<>();
            List<String> gs = new ArrayList<>();
        //    g.add("CDJ4F277");
            createAppCoupon.getData("123sd1aco75o","20","17732141036",
                    "50","20200507","20200609",g,gs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
